import java.util.LinkedList;
import java.util.Queue;

class FIFOTransactionQueue {
    private Queue<Double> transactions;
    private double balance;

    public FIFOTransactionQueue() {
        transactions = new LinkedList<>();
        balance = 0;
    }

    public void enQueue(double amount) {
        transactions.add(amount);
        balance += amount;
        System.out.println("Enqueued: " + amount + ", Current Balance: " + balance);
    }

    public void deQueue(double amount) {

        if (balance < amount || balance <= 0) {
            System.out.println("余额不足");
            return;
        }

        // 余额充足，扣减余额
        double amountToDequeue = transactions.poll();
        balance -= amountToDequeue;
        // 队列的金额不够取
        while (amountToDequeue < amount) {
            // 扣减一笔
            amount -= amountToDequeue;
            // 再次取出一笔
            amountToDequeue = transactions.poll();
            balance -= amountToDequeue;
        }

        // 去列中的金额够取了，有刚好够和大于2中可能
        // 大于需要把扣减后的金额添加到队列
        // 等于说明刚好够扣，不需要处理
        if (amountToDequeue > amount) {
            transactions.add(amountToDequeue - amount);
            balance += (amountToDequeue - amount);
        }

        System.out.println("余额为：" + balance);

    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        FIFOTransactionQueue transactionQueue = new FIFOTransactionQueue();

        transactionQueue.enQueue(100);
        transactionQueue.enQueue(50);
        transactionQueue.enQueue(60);

        System.out.println("Initial Balance: " + transactionQueue.getBalance());

        transactionQueue.deQueue(100);
        transactionQueue.deQueue(70);

        System.out.println("Final Balance: " + transactionQueue.getBalance());
    }
}
